<template>
  <div class="container">
    <div class="chart">
      <LakerEcharts ref="wchart" :option="option" width="100%" height="100%" />
    </div>
  </div>
</template>
<script>
import LakerEcharts from '@comp/lakerEcharts/index.vue'
import dprevent from '@/api/dprevent'
export default {
  name: 'DpCenterItem1',
  components: {
    LakerEcharts
  },
  data() {
    return {
      option: {
        grid: {
          top: '40',
          left: '40',
          right: '10',
          bottom: '30'
        },
        tooltip: {
          trigger: 'axis',
          formatter: function (params) {
            var ta0 = params[0]
            var tar = params[1]
            return ta0.name + '</br>' + ta0.seriesName + ' : ' + ta0.data + '</br>' + tar.seriesName + ' : ' + tar.data
          }
        },
        xAxis: {
          type: 'category',
          data: ['公司级', '部门级', '班组级', '岗位级']
        },
        yAxis: {
          type: 'value',
          name: '单位：个'
        },
        series: [
          {
            name: '本月',
            type: 'bar',
            barWidth: 20,
            stack: 'Ad',
            label: {
              show: true,
              position: 'top',
              color: '#ccc'
            },
            emphasis: {
              focus: 'series'
            },
            showBackground: true,
            data: [165, 187, 162, 162]
          },
          {
            name: '今日',
            type: 'bar',
            barWidth: 20,
            stack: 'Ad2',
            label: {
              show: true,
              position: 'top',
              color: '#ccc'
            },
            emphasis: {
              focus: 'series'
            },
            showBackground: true,
            data: [65, 87, 62, 162]
          }
        ]
      }
    }
  },
  mounted() {
    this.getApiData()
    setInterval(() => {
      this.getApiData()
    }, 5000)
  },
  methods: {
    getApiData() {
      dprevent.dpleftapi2().then((res) => {
        if (res.code === 'success') {
          if (res.result) {
            let data = res.result
            this.option.xAxis.data = data.map((item) => item.name)
            this.option.series[0].data = data.map((item) => item.mvalue)
            this.option.series[1].data = data.map((item) => item.dvalue)
          }
        }
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.container {
  display: flex;
  height: 100%;
  width: 98%;
  .chart {
    width: 100%;
    height: 100%;
  }
}
</style>
